#include <iostream>
#include <memory>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "HttpServer.hpp"
#include "Util.hpp"
#include "Daemon.hpp"

// 一般http都要有自己的web根目录,下面让请求的资源从web根目录开始,这就是为什么叫作web根目录
#define ROOT "./wwwroot" // ./wwwroot/index.html
#define HOMEPAGE "index.html" // 如果客户端只请求了一个/,返回默认首页

static void Usage(const std::string &process) // 使用手册
{
    std::cout << "\nUsage: " << process << " port\n" << std::endl;
}

void HandlerHttpRequest(int sockfd)
{
    // 1. 读取请求并打印
    char buffer[10240];
    ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (s > 0)
    {
        buffer[s] = 0;
        logMessage(NORMAL, "%s", buffer);
        std::cout << buffer << "--------------------\n" << std::endl; // 把请求打印出来,加点分隔符
    }

    std::vector<std::string> vline; // 剪切读取到的请求
    Util::cutString(buffer, "\n", &vline); // 读取第一行

    std::vector<std::string> vblock;
    Util::cutString(vline[0], " ", &vblock); // 第一行拆开

    // std::cout << "#### start ################" << std::endl;
    // for(auto &iter : vblock)
    // {
    //     std::cout << "---" << iter << "\n" << std::endl;
    // }
    // std::cout << "##### end ###############" << std::endl;

    std::string file = vblock[1]; // 就是请求路径,类似/a/b/c.html
    std::string target = ROOT; // 定义成自己宏定义的web根目录

    if (file == "/") // 如果请求的是web根目录(类似默认目录)
        file = "/index.html"; // 默认目录改成这个
    target += file;
    std::cout << target << std::endl;

    std::string content; // 存文件内容的
    std::ifstream in(target); // 打开target路径文件
    if (in.is_open()) // 成功打开
    {
        std::string line;
        while (std::getline(in, line))
        {
            content += line;
        }
        in.close();
    }

    std::string HttpResponse; // 构建响应,现在先简单用用 // 后面还会讲解
    if (content.empty()) // 状态行->没有内容
    {
        HttpResponse = "HTTP/1.1 301 Moved Permanently\r\n";
        // HttpResponse += "Location: https://blog.csdn.net/GRrtx?type=blog\r\n"; // 重定向
        HttpResponse += "Location: http://121.199.6.56:7070/a/b/404.html\r\n"; // 站内跳转
    }
    else // 有内容(正常)
    {
        HttpResponse = "HTTP/1.1 200 OK\r\n";
        HttpResponse += ("Content-Type: text/html\r\n"); // 要识别,有些浏览器能自动识别,不过现在是网页,可以是照片和视频等
        HttpResponse += ("Content-Length: " + std::to_string(content.size()) + "\r\n");
        HttpResponse += "Set-Cookie: 这是一个cookie(小块的东西)\r\n";
    }
    HttpResponse += "\r\n"; // 空行
    HttpResponse += content; //正文

    // // 2. 试着构建一个http的响应并发送
    // std::string HttpResponse = "HTTP/1.1 200 OK\r\n"; // 状态行
    // HttpResponse += "\r\n"; // 空行
    // HttpResponse += "<html><h1>Hello Linux NetWork GR_C</h1></html>"; // 正文
    send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    MyDaemon();
    std::unique_ptr<HttpServer> httpserver(new HttpServer(atoi(argv[1]), HandlerHttpRequest));
    httpserver->Start();
    return 0;
}

/*
#include <iostream>
#include <memory>
#include <cassert>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "HttpServer.hpp"
#include "Usage.hpp"
#include "Util.hpp"

// 一般http都要有自己的web根目录
#define ROOT "./wwwroot" // ./wwwroot/index.html
// 如果客户端只请求了一个/,我们返回默认首页
#define HOMEPAGE "index.html"

void HandlerHttpRequest(int sockfd)
{
    // 1. 读取请求 for test
    char buffer[10240];
    ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (s > 0)
    {
        buffer[s] = 0;
    }
    std::cout << buffer << "\n--------------------\n" << std::endl;

    std::vector<std::string> vline;
    Util::cutString(buffer, "\n", &vline);

    std::vector<std::string> vblock;
    Util::cutString(vline[0], " ", &vblock);

    std::string file = vblock[1]; // /a/b/c.html
    std::string target = ROOT;

    if(file == "/") file = "/index.html";
    // wwwroot/index.html
    // wwwroot/a/b/c.html
    target += file;
    std::cout << target << std::endl;

    std::string content;
    std::ifstream in(target);
    if(in.is_open())
    {
        std::string line;
        while(std::getline(in, line))
        {
            content += line;
        }
        in.close();
    }

    std::string HttpResponse;
    if(content.empty()) 
    {
        HttpResponse = "HTTP/1.1 301 Moved Permanently\r\n";
        HttpResponse += "Location: http://120.78.126.148:8081/a/b/404.html\r\n";
    }
    else 
    {
        HttpResponse = "HTTP/1.1 200 OK\r\n";
        HttpResponse += ("Content-Type: text/html\r\n"); 
        HttpResponse += ("Content-Length: " + std::to_string(content.size()) + "\r\n");
        HttpResponse += "Set-Cookie: 这是一个cookie\r\n";
    }
    HttpResponse += "\r\n";
    HttpResponse += content;
        // std::cout << "####start################" << std::endl;
        // for(auto &iter : vblock)
        // {
        //     std::cout << "---" << iter << "\n" << std::endl;
        // }
        // std::cout << "#####end###############" << std::endl;

        // 2. 试着构建一个http的响应
    send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::unique_ptr<HttpServer> httpserver(new HttpServer(atoi(argv[1]), HandlerHttpRequest));
    httpserver->Start();
    return 0;
}
*/